package com.example.administrator.im.view;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.AppCompatTextView;
import android.text.Layout;
import android.util.AttributeSet;
import android.util.Log;

/**
 * create:2018/11/10 10:02
 * author: 姚鸿杰
 **/
public class LastLineNoSpaceTextView extends AppCompatTextView {
    private Rect mRect;

    public LastLineNoSpaceTextView(Context context) {
        this(context, null);
    }

    public LastLineNoSpaceTextView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public LastLineNoSpaceTextView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        mRect = new Rect();
    }

    protected void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        int measuredHeight = getMeasuredHeight() - getLastLineSpace();
        setMeasuredDimension(getMeasuredWidth(), measuredHeight);
    }

    public int getLastLineSpace() {
        int lastLineIndex = getLineCount() - 1;
        if (lastLineIndex < 0) {
            return 0;
        }
        Layout layout = getLayout();
        int baseline = getLineBounds(lastLineIndex, mRect);
//        if (getMeasuredHeight() != layout.getHeight()) {
//            return 0;
//        }
        int fontHeight = baseline + layout.getPaint().getFontMetricsInt().descent;
        int lineHeight = mRect.bottom;
        return lineHeight - fontHeight;
    }

}
